#include "AdaptiveAlgorithmBase.h"
#include "Utils.h"

#include <sstream>

namespace RafMat
{
namespace SimEngine
{

using namespace std;
using namespace SimInterface;

const string AdaptiveAlgorithmBase::CONF_RPOLY("rpoly");
const string AdaptiveAlgorithmBase::CONF_SPOLY("spoly");
const string AdaptiveAlgorithmBase::CONF_TPOLY("tpoly");
const string AdaptiveAlgorithmBase::CONF_H("h");


AdaptiveAlgorithmBase::AdaptiveAlgorithmBase(RSTController& owner, std::string type,
                                             Polynomial r, Polynomial s, Polynomial t, double h)
    :  m_r(r), m_s(s), m_t(t), m_h(h), m_owner(owner), m_type(type)
{
}


AdaptiveAlgorithmBase::AdaptiveAlgorithmBase(RSTController& owner, std::string type, std::string name,
                                             Polynomial r, Polynomial s, Polynomial t, double h)
    : IConfigurable(name), m_r(r), m_s(s), m_t(t), m_h(h), m_owner(owner), m_type(type)
{
}

//
// Sprawy konfiguracyjne
//

// setParameter nie implementujemy, klasa i tak jest abstrakcyjna, a R, S, T są tylko do odczytu (domyślnie).

std::string AdaptiveAlgorithmBase::getParameter(const std::string& param) const throw(std::logic_error)
{
    typedef ostream_iterator<double> oit;

    string tparam = Utils::trimWhiteSpaces(param);

    string tmpOut;
    ostringstream ostr(tmpOut);

    if(tparam == CONF_RPOLY)
    {
        Polynomial tmpPoly = m_r;
        ostr << "[ ";
        copy(tmpPoly.begin(), tmpPoly.end(), oit(ostr, " "));
        ostr << ']';
    }
    else if(tparam == CONF_SPOLY)
    {
        Polynomial tmpPoly = m_s;
        ostr << "[ ";
        copy(tmpPoly.begin(), tmpPoly.end(), oit(ostr, " "));
        ostr << ']';
    }
    else if(tparam == CONF_TPOLY)
    {
        Polynomial tmpPoly = m_t;
        ostr << "[ ";
        copy(tmpPoly.begin(), tmpPoly.end(), oit(ostr, " "));
        ostr << ']';
    }
    else if(tparam == CONF_H)
    {
        ostr << m_h;
    }
    else
        throw iconf_error::unrecognizedParam(*this, tparam);

    return ostr.str();
}

SimFile::ConfigWriter& AdaptiveAlgorithmBase::dumpParameters(SimFile::ConfigWriter& o) const
{
    // Nie rób nic
    return o;
}



}
}
